import express from 'express';
import { queryChatHistory, toRead } from '../service/chat.js';

const chatRouter = express.Router();

chatRouter.get('/queryUnRead', (req, res) => {
  const { query: data } = req;
  queryChatHistory(data.sender, data.receiver).then((result) => {
    const newRes = result.filter((item) => {
      if (item) return item.sender === data.sender && item.receiver === data.receiver && item.isRead === 0;
    });
    res.send({ resCode: 200, data: newRes });
  });
});

chatRouter.post('/toRead', (req, res) => {
  const { body: data } = req;
  toRead(data.sender, data.receiver);
  res.send({ resCode: 200 });
});

export default chatRouter;
